package com.yanqu.road.server.cmd.cross.tradewar;

import com.yanqu.road.entity.config.battle.BattleResult;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.tradewar.CrossUserTradeWarBattle;
import com.yanqu.road.entity.tradewar.UserTradeWarInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.cross.tradewar.CrossTradeWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.TradeWarModule;
import com.yanqu.road.logic.pb.CrossTradeWarPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;


@Cmd(code = Protocol.S_CROSS_TRADE_WAR_PREPARE_NOTIFY, desc = "跨服商战匹配通知")
public class CrossTradeWarPrepareNotifyCmd extends AbstractCommand {
	private int activityId;
	private long myUserId;
	private long myServerId;
	private int result;
	private int battleType;
	private List<Integer> patronsIdList;
	private long enemyUserId;
	private long enemyScore;
	private int enemyPatronsCount;
	private String battleUid;
	private int useItemId;
	private UserBaseInfo enemyBaseInfo;
	private int wantedItemId;
	private long publishWantedUserId;
	private CrossTradeWarProto.CrossTradeWarPrepareRespMsg reqMsg;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		reqMsg = CrossTradeWarProto.CrossTradeWarPrepareRespMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		myUserId = reqMsg.getMyUserId();
		myServerId = reqMsg.getMyServerId();
		result = reqMsg.getRet();
		battleType = reqMsg.getBattleType();
		patronsIdList = new ArrayList<>(reqMsg.getPatronsIdListList());
		enemyUserId = reqMsg.getEnemyUserId();
		battleUid = reqMsg.getBattleUid();
		useItemId = reqMsg.getUseItemId();
		if(0 == result) {
			enemyScore = reqMsg.getEnemyScore();
			enemyPatronsCount = reqMsg.getEnemyPatronsCount();
			enemyBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getEnemyBaseInfo());
			wantedItemId = reqMsg.getWantedItemId();
			publishWantedUserId = reqMsg.getPublishWantedUserId();
		}
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if(result != 0){
			ret = result;
			return;
		}
		ret = player.getModule(TradeWarModule.class).crossPrepareNotify(activityId, battleType, patronsIdList, useItemId, enemyUserId,
				enemyScore, enemyPatronsCount, battleUid, enemyBaseInfo, wantedItemId, publishWantedUserId, reqMsg.getFastTeamNum());
	}

	@Override
	public void sendData(GamePlayer player) {
		if (reqMsg.getFastTeamNum() >= 0) {
			if (ret != 0){
				CrossTradeWarProto.CrossTradeWarFastBattleRespMsg.Builder respMsg = CrossTradeWarProto.CrossTradeWarFastBattleRespMsg.newBuilder();
				respMsg.setRet(ret);
				respMsg.setActivityId(activityId);
				respMsg.setTeamNum(reqMsg.getFastTeamNum());
				player.sendPacket(Protocol.U_CROSS_TRADE_WAR_FAST_BATTLE_START, respMsg);
			}else {
				player.sendPacket(Protocol.S_CROSS_TRADE_WAR_BATTLE_START, CrossTradeWarProto.CrossTradeWarBattleStartReqMsg.newBuilder().setActivityId(activityId));
			}
		}else {
			CrossTradeWarProto.CrossTradeWarBattleRespMsg.Builder respMsg = CrossTradeWarProto.CrossTradeWarBattleRespMsg.newBuilder();
			respMsg.setRet(ret);
			if (0 == ret) {
				long score = 0;
				UserTradeWarInfo userTradeWarInfo = player.getModule(TradeWarModule.class).getUserTradeWarInfo();
				if (null != userTradeWarInfo) {
					score = userTradeWarInfo.getCrossScore();
				}
				CrossUserTradeWarBattle crossUserTradeWarBattle = player.getModule(TradeWarModule.class).getCrossUserTradeWarBattle();
				CrossTradeWarProto.CrossTradeWarBattleTempMsg.Builder msg = CrossTradeWarPb.parseCrossTradeWarBattleTempMsg(crossUserTradeWarBattle, score);
				respMsg.setBattleInfo(msg);
				respMsg.addAllEnemyPatronsList(reqMsg.getEnemyPatronsListList());
				respMsg.setEnemyPatronsAbility(reqMsg.getEnemyPatronsAbility());
			}
			player.sendPacket(Protocol.U_CROSS_TRADE_WAR_BATTLE_PREPARE, respMsg);
		}


	}

}
